#include "optionspanel.h"

OptionsPanel::OptionsPanel(QWidget *parent)
    : QWidget(parent)
{
    resize(450,450);

    optionsScene = new QGraphicsScene(0,0,450,450,this);

    optionsView = new QGraphicsView(optionsScene,this);
    optionsView->resize(450,450);
    optionsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    optionsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    background = new GameBackground(QPixmap(RESOURCE_DIR + "/Kit_MenuBackground_1.png"),optionsScene);

    setVisible(false);

    title = optionsScene->addText("Game Paused",QFont("Arial",40));
    title->setPos(width()/2-title->boundingRect().width()/2,12);
    title->setDefaultTextColor(QColor(255,255,255));

    // Setup the menu
    endItem = new MenuItem(QPixmap(RESOURCE_DIR + "/menuImages/Kit_EndItem.png"),
                        QPixmap(RESOURCE_DIR + "/menuImages/Kit_EndItemOver.png"),
                        QPixmap(RESOURCE_DIR + "/menuImages/Kit_EndItemSelected.png"),
                        optionsScene);

    helpItem = new MenuItem(QPixmap(RESOURCE_DIR + "/menuImages/Kit_HelpItem_1.png"),
                        QPixmap(RESOURCE_DIR + "/menuImages/Kit_HelpItemOver_1.png"),
                        QPixmap(RESOURCE_DIR + "/menuImages/Kit_HelpItemSelected_1.png"),
                        optionsScene);

    quitItem = new MenuItem(QPixmap(RESOURCE_DIR + "/menuImages/Kit_QuitItem_1.png"),
                        QPixmap(RESOURCE_DIR + "/menuImages/Kit_QuitItemOver_1.png"),
                        QPixmap(RESOURCE_DIR + "/menuImages/Kit_QuitItemSelected_1.png"),
                        optionsScene);

    resumeItem = new MenuItem(QPixmap(RESOURCE_DIR + "/menuImages/Kit_ResumeItem.png"),
                              QPixmap(RESOURCE_DIR + "/menuImages/Kit_ResumeItemOver.png"),
                              QPixmap(RESOURCE_DIR + "/menuImages/Kit_ResumeItemSelected.png"),
                              optionsScene);

    menu.append(endItem);
    menu.append(helpItem);
    menu.append(quitItem);
    menu.append(resumeItem);

    for (int index = 0; index < menu.length(); index++)
    {
    menu.at(index)->setPos((width()/2)-(menu.at(index)->pixmap().width()/2),
                       (height()/2)+((menu.at(index)->pixmap().height()+14)*(menu.length()/(2.00-menu.length())))+((menu.at(index)->pixmap().height()+14)*index));

    QObject::connect(menu.at(index),SIGNAL(clicked(MenuItem*)),this,SLOT(itemClicked(MenuItem*)));
    }
}

void OptionsPanel::nextScene()
{
    if (menuPosition == 0)
        emit endScene(GameScene::menuScene);

    else if (menuPosition == 1)
        emit endScene(GameScene::helpScene);

    else if (menuPosition == 2)
        emit endScene(GameScene::endGame);

    else if (menuPosition == 3)
    {
        setEnabled(false);
        emit resumeScene();
    }

}

void OptionsPanel::keyPressEvent(QKeyEvent * key)
{
    // Catch movement keys
    if ((key->key() == Qt::Key_Down) && (menuPosition < menu.length()))
    {
        menu.at(menuPosition)->setActive(false);
        menuPosition++;
        menu.at(menuPosition)->setActive(true);
    }
    else if ((key->key() == Qt::Key_Up) && (menuPosition > 0))
    {
        menu.at(menuPosition)->setActive(false);
        menuPosition--;
        menu.at(menuPosition)->setActive(true);
    }

    // catch the enter key
    else if ((key->key() == Qt::Key_Enter) || (key->key() == Qt::Key_Return))
    {
        nextScene();
    }
}

void OptionsPanel::itemClicked(MenuItem *item)
{
    menuPosition = menu.indexOf(item);
    nextScene();
}

void OptionsPanel::setEnabled(bool enabled)
{
    menuPosition=0;
    menu.at(0)->setActive(true);
    setVisible(enabled);
    optionsView->setInteractive(enabled);

    if (enabled)
        grabKeyboard();
    else
        releaseKeyboard();
}
